package jumpingnotes.dao.hibernate;

import jumpingnotes.dao.AudioDao;
import jumpingnotes.model.entity.Audio;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

public class AudioHibernateDao extends GenericHibernateDao<Audio> implements AudioDao
{
	public AudioHibernateDao()
	{
		super(Audio.class);
	}

	@SuppressWarnings("unchecked")
	public Audio findAudioByUuid(final String uuid)
	{
		return (Audio) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Query query = session.createQuery("select distinct a from Audio a where a.uuid=:uuid");
				query.setString("uuid", uuid);
				return query.uniqueResult();
			}
		});
	}

	@Override
	public Audio findById(final int audioId)
	{
		return (Audio) getHibernateTemplate().execute(new HibernateCallback()
		{
			public Object doInHibernate(Session session) throws HibernateException
			{
				Criteria criteria = session.createCriteria(Audio.class);
				criteria.add(Restrictions.eq("audioId", audioId));
				criteria.setFetchMode("sync", FetchMode.JOIN);
				return criteria.uniqueResult();
			}
		});
	}
}
